package com.followmeandroid.util;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.followmeandroid.enums.ServiceUrl;
import com.followmeandroid.models.CurrentMember;
import com.followmeandroid.models.Member;
import com.followmeandroid.models.SingleToken;
import com.followmeandroid.models.Token;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Adam on 11/22/2014.
 */
public class FollowMeSharedPreferences {

    private static android.content.SharedPreferences mPrefs;

    public static void init(Context _context) {
        mPrefs = _context.getSharedPreferences("Main", Context.MODE_PRIVATE);
    }

    public static synchronized Member loadMember() {
        Gson gson = new Gson();
        String currentMember = mPrefs.getString("CurrentMember", null);
        String json = mPrefs.getString(currentMember, null);

        Member member = new GsonBuilder().create().fromJson(json, Member.class);
        CurrentMember.setMember(member);
        return member;
    }

    public static synchronized void saveMember() {
        if (CurrentMember.getMember() != null) {
            android.content.SharedPreferences.Editor prefsEditor = mPrefs.edit();
            Gson gson = new Gson();
            String json = gson.toJson(CurrentMember.getMember());
            prefsEditor.putString("CurrentMember", CurrentMember.getMember().getUserName());
            prefsEditor.putString(CurrentMember.getMember().getUserName(), json);
            prefsEditor.commit();
        }
    }

    public static synchronized Token loadToken() {
        Gson gson = new Gson();
        String json = mPrefs.getString("Token", null);
        Token tok = new GsonBuilder().create().fromJson(json, Token.class);
        SingleToken.setToken(tok);
        return tok;
    }

    public static synchronized void saveToken() {
        android.content.SharedPreferences.Editor prefsEditor = mPrefs.edit();
        Gson gson = new Gson();
        String json = gson.toJson(SingleToken.getToken());
        prefsEditor.putString("Token", json);
        prefsEditor.commit();
    }


    public static synchronized  void reloadMember(Context context){
        AsyncReloadUser reload = new AsyncReloadUser();
        reload.setContext(context);
        reload.execute();
    }



    protected static class AsyncReloadUser extends AsyncTask<Void, JSONObject, Boolean> {
        Context context;

        public void setContext(Context context) {
            this.context = context;
        }

        @Override
        protected Boolean doInBackground(Void... params) {

            JSONHttpClient api = new JSONHttpClient();
            try {
                    //get memberId from Token
                    List<NameValuePair> paramsOut = new ArrayList<NameValuePair>();
                    paramsOut.add(new BasicNameValuePair("memberId", SingleToken.getToken().getMemberId().toString()));

                    //get member data (all of it) from the server.
                    Member serverMember = api.Get(ServiceUrl.GET_ALL_INFO.toString(), paramsOut, Member.class);

                    if (serverMember != null) {
                        CurrentMember.setMember(serverMember);
                        FollowMeSharedPreferences.saveMember();
                    } else
                        return false;
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.d("AsyncLoadMember", e.getMessage());
            }

            return true;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Toast.makeText(context, "Please Wait...", Toast.LENGTH_SHORT).show();
        }

        /**
         * @param result boolean of reload results
         */
        @Override
        protected void onPostExecute(Boolean result) {
            if (!result) {
                Toast.makeText(context, "Can't get user info atm :P", Toast.LENGTH_SHORT).show();
            }
        }
    }
}
